package com.ysm.smfu.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.ysm.smfu.R;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.fragment.BankFragment;
import com.ysm.smfu.fragment.BankFragment2;
import com.ysm.smfu.fragment.LoginPsdSetFragment;
import com.ysm.smfu.fragment.LoginPsdSetNextFragment;
import com.ysm.smfu.fragment.MyBankFragment;
import com.ysm.smfu.fragment.MyYuErFragment;
import com.ysm.smfu.fragment.MyZiLiaoFragment;
import com.ysm.smfu.fragment.PhoneNumSetFragment;
import com.ysm.smfu.fragment.PhoneNumSetNextFragment;
import com.ysm.smfu.fragment.QrCodeFragment;
import com.ysm.smfu.fragment.ZhiFuFragment;
import com.ysm.smfu.utils.UIUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/9/26.
 */
public class My_XiuGai_Activity extends BaseActivity {
    @BindView(R.id.title_body)
    TextView titleBody;
    @BindView(R.id.mainActivity_topbar_img)
    ImageView mainActivityTopbarImg;
    @BindView(R.id.activity_my_xiugai)
    FrameLayout activityMyXiugai;
    private int position;
    private BankFragment bankFragment;
    private View view;
    private String zhifupsd;
    private String loginPsd;

    @Override
    protected void init() {
        super.init();
        position = getIntent().getIntExtra("position", 0);
        zhifupsd = getIntent().getStringExtra(SpKey.ZHIFUPSD);
        loginPsd = getIntent().getStringExtra("loginPsd");
    }

    @Override
    protected void initViews() {
        super.initViews();
        view = UIUtils.inflate(R.layout.activity_my_xiugai);
        ImageView back = (ImageView) view.findViewById(R.id.back);
//        调用系统后退方法，有软键盘时，先关闭软键盘
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        setContentView(view);
        ButterKnife.bind(this);
        setTopBarImgGone();
        titleBody.setText("我的");
    }

    Fragment fragment;

    @Override
    protected void initData() {
        super.initData();
        switch (position) {
            case 0:
                // 修改个人资料
                titleBody.setText("个人资料");
                fragment = new MyZiLiaoFragment();
                break;
            case 1:
                titleBody.setText("余额");
                double yuer = getIntent().getDoubleExtra("yuer", 0.00);
                fragment = new MyYuErFragment();
                Bundle bundlee = new Bundle();
                bundlee.putDouble("yuer", yuer);
                fragment.setArguments(bundlee);
                break;
            case 2:
                titleBody.setText("我的二维码");
                fragment = new QrCodeFragment();
                //        是否是展示邀请好友的二维码
                boolean isInvit = getIntent().getBooleanExtra("invit", false);
                if (isInvit) {
                    titleBody.setText("邀请好友");
                    String invitUrl = getIntent().getStringExtra(SpKey.INVITURL);
                    Bundle bundle = new Bundle();
                    bundle.putString(SpKey.INVITURL, invitUrl);
                    fragment.setArguments(bundle);
                }
                break;
            case 3:
                titleBody.setText("设置支付密码");
                fragment = new ZhiFuFragment();
//                判断是从银行卡选项进入还是从设置支付密码选项进入
                Bundle bundle = new Bundle();
                bundle.putString("function", "bank");
                fragment.setArguments(bundle);
                break;
            case 8:
                titleBody.setText("我的银行卡");
//                显示管理银行卡页面
                this.fragment = new MyBankFragment();
                bankFragment = new BankFragment();
                mainActivityTopbarImg.setVisibility(View.VISIBLE);
                mainActivityTopbarImg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        titleBody.setText("添加银行卡");
                        toggleFragment(bankFragment);
                    }
                });
                break;
            case 4:
                titleBody.setText("添加银行卡");
                this.fragment = new BankFragment2();
                break;
            case 5:
                this.fragment = new LoginPsdSetFragment();
                if (TextUtils.equals(loginPsd, "loginPsd")) {
                    titleBody.setText("找回登录密码");
                    Bundle bundle1 = new Bundle();
                    bundle1.putString("loginPsd", loginPsd);
                    fragment.setArguments(bundle1);
                } else
                    titleBody.setText("修改登陆密码");
                break;
            case 6:
                titleBody.setText("修改登陆密码");
                String code = getIntent().getStringExtra(SpKey.CODE);
                String phone = getIntent().getStringExtra(SpKey.PHONE);
                String loginPsd = getIntent().getStringExtra("loginPsd");
                this.fragment = new LoginPsdSetNextFragment();
                Bundle bundle1 = new Bundle();
                bundle1.putString(SpKey.CODE, code);
                bundle1.putString(SpKey.PHONE, phone);
                bundle1.putString("loginPsd", loginPsd);
                fragment.setArguments(bundle1);
                break;
            case 7:
                titleBody.setText("修改手机号");
                this.fragment = new PhoneNumSetFragment();

                break;
            case 9:
                titleBody.setText("修改手机号");
                this.fragment = new PhoneNumSetNextFragment();
//                新手机修改完成，返回我的页面
                break;
            case 10:
                titleBody.setText("设置支付密码");
                this.fragment = new ZhiFuFragment();
                break;
            case 16:
                titleBody.setText("找回密码");
                this.fragment = new LoginPsdSetNextFragment();
                Bundle bundle2 = new Bundle();
                bundle2.putString(SpKey.ZHIFUPSD, zhifupsd);
                fragment.setArguments(bundle2);
                break;

        }
        if (fragment != null)
            toggleFragment(fragment);
    }

    void toggleFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction().replace(R.id.activity_my_xiugai, fragment).commit();
    }

    /**
     * 设置加号隐藏
     */
    public void setTopBarImgGone() {
        mainActivityTopbarImg.setVisibility(View.GONE);
    }

    /**
     * 获取后退键
     *
     * @return
     */
    public ImageView getBack() {
        return (ImageView) view.findViewById(R.id.back);
    }

}
